package com.xfhy.wallpaperdemo.live;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.os.PowerManager;

import com.xfhy.wallpaperdemo.DaemonLog;
import com.xfhy.wallpaperdemo.MyApplication;
import com.xfhy.wallpaperdemo.RomUtil;
import com.xfhy.wallpaperdemo.account.AccountHelper;

import java.util.ArrayList;
import java.util.List;

/* renamed from: pk  reason: default package */
public class pk {
    public static final int QUERY_INTERVAL = 50;
    public static final int QUERY_INTERVAL_LARGE = 1000;
    public static int c = 50;
    public c a;
    public List<b> b;

    /* renamed from: pk$b */
    public interface b {
        void onScreenStatusChanged(boolean z2);
    }

    /* renamed from: pk$c */
    public static class c extends Thread {
        public static final int e = 1;
        public static final int f = 2;
        public static final int g = 3;
        public PowerManager a;
        public boolean b;
        public volatile int c = 3;
        public KeyguardManager d;

        public c() {
            @SuppressLint("WrongConstant") PowerManager powerManager = (PowerManager) MyApplication.sContext.getSystemService("power");
            this.a = powerManager;
            if (powerManager != null) {
                this.b = powerManager.isScreenOn();
            }
        }

        public synchronized void a() {
            DaemonLog.d("ScreenMonitor pauseMonitor,cur status=" + this.c);
            if (this.c == 1) {
                this.c = 2;
                DaemonLog.d("ScreenMonitor pauseMonitor success");
            }
        }

        public synchronized void b() {
            DaemonLog.d("ScreenMonitor resumeMonitor,cur status=" + this.c);
            if (this.c == 2) {
                this.c = 1;
                notify();
                DaemonLog.d("ScreenMonitor resumeMonitor success");
            }
        }

        public synchronized void c() {
            DaemonLog.d("ScreenMonitor startMonitor,cur status=" + this.c);
            if (this.c != 1) {
                this.c = 1;
                start();
                notify();
                DaemonLog.d("ScreenMonitor startMonitor success");
            }
        }

        public synchronized void d() {
            DaemonLog.d("ScreenMonitor stopMonitor,cur status=" + this.c);
            if (this.c != 3) {
                this.c = 3;
                DaemonLog.d("ScreenMonitor stopMonitor success");
            }
        }

        @Override
        public void run() {
            while (this.c != 3) {
                synchronized (this) {
                    while (this.c != 1) {
                        try {
                            wait();
                        } catch (InterruptedException e2) {
                            DaemonLog.d("ScreenMonitor wait InterruptedException", e2);
                        }
                    }
                }
                boolean isScreenOn = this.a.isScreenOn();
                if (this.b != isScreenOn) {
                    this.b = isScreenOn;
                    if (!isScreenOn && this.d != null) {
                        DaemonLog.d("ScreenMonitor dispatchScreenChanged,isScreenOn=false,isKeyguardLocked=" + this.d.isKeyguardLocked());
                    }
                    pk.getInstance().a(isScreenOn);
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e3) {
                    DaemonLog.d("ScreenMonitor InterruptedException", e3);
                }
            }
            DaemonLog.d("ScreenMonitor status == STATUS_STOPED,exit");
        }
    }

    /* renamed from: pk$d */
    public static class d {
        public static final pk a = new pk();
    }

    public static pk getInstance() {
        return d.a;
    }

    public static void setQueryInterval(int i) {
        c = i;
    }

    public synchronized void addCallback(b bVar) {
        if (this.b == null) {
            this.b = new ArrayList();
        }
        if (!this.b.contains(bVar)) {
            this.b.add(bVar);
        }
    }

    public synchronized void pause() {
        if (this.a != null) {
            this.a.a();
        }
    }

    public synchronized void removeCallback(b bVar) {
        if (this.b != null) {
            this.b.remove(bVar);
        }
    }

    public synchronized void resume() {
        if (this.a != null) {
            this.a.b();
        }
    }

    public synchronized void start() {
        if (this.a == null || !this.a.isAlive()) {
            this.a = new c();
        }
        this.a.c();
    }

    public synchronized void stop() {
        if (this.a != null) {
            this.a.d();
        }
    }

    public pk() {
    }

    /* access modifiers changed from: private */
    public synchronized void a(boolean z2) {
        if (this.b != null) {
            for (b onScreenStatusChanged : this.b) {
                onScreenStatusChanged.onScreenStatusChanged(z2);
            }

            //单独增加的  其实不应该放这里
            if (!RomUtil.isOppo() || z2) {
                nk.getInstance().a();
            } else {
                AccountHelper.cancelSync(MyApplication.sContext);
            }

        }
    }
}
